<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="dept">
        <el-input
          v-model="queryParams.dept"
          placeholder="请输入部门"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入公司"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['company:mail:outside:add']"
          >新增外部通讯录</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table fit v-loading="loading" border :data="dataList">
      <el-table-column label="序号" type="index" width="120" />
      <el-table-column label="姓名" prop="name" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link @click="handleDetail(scope.row)" type="primary">{{
            scope.row.name
          }}</el-link>
        </template>
      </el-table-column>

      <el-table-column label="职位" :show-overflow-tooltip="true" prop="post" />
      <el-table-column
        label="单位/部门"
        align="center"
        min-width="200"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ scope.row.company }}/ {{ scope.row.dept }}
        </template>
      </el-table-column>
      <el-table-column
        label="手机号码"
        :show-overflow-tooltip="true"
        prop="phone"
        width="150"
      />
      <el-table-column
        label="联系电话"
        :show-overflow-tooltip="true"
        prop="telephone"
        width="150"
      />
      <el-table-column label="邮箱" prop="email" width="180" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            v-hasPermi="['company:mail:outside:info']"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['company:mail:outside:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:mail:outside:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改公司公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form
        :disabled="disabled"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入姓名"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="公司" prop="company">
          <el-input
            v-model="form.company"
            placeholder="请输入公司"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="dept">
          <el-input
            v-model="form.dept"
            placeholder="请输入部门"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="post">
          <el-input
            v-model="form.post"
            placeholder="请输入职位"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input
            v-model="form.telephone"
            placeholder="请输入联系电话"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="form.email"
            placeholder="请输入邮箱"
            clearable
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  bookList,
  bookAdd,
  statusChange,
  bookEdit,
  bookDel,
} from "@/api/company/outside.js";
export default {
  name: "Outside",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      disabled: false,
      title: "新增外部通讯录",
      dataList: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      form: {
        status: 0,
        bookType: "1",
      },
      // 表单校验
      rules: {
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
        ],
        company: [
          {
            required: true,
            message: "请输入公司",
            trigger: "blur",
          },
        ],
        dept: [
          {
            required: true,
            message: "请输入部门",
            trigger: "blur",
          },
        ],
        post: [
          {
            required: true,
            message: "请输入职位",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        telephone: [
          {
            required: false,
            message: "请输入联系电话",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: false,
            message: "请输入邮箱",
            trigger: "blur",
          },
        ],
      },
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookTitle: undefined,
        busType: 2,
      },
      rowDetail: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 提交按钮 */
    submitForm: function () {
      if (this.disabled) {
        this.open = false;

        return;
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.addressBookId != undefined) {
            bookEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            bookAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDetail(row) {
      this.open = true;
      this.form = JSON.parse(JSON.stringify(row));
      this.disabled = true;
      this.title = "查看外部通讯录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm("form");
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = "编辑外部通讯录";
      this.disabled = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除姓名为"' + row.name + '"的数据项？')
        .then(function () {
          return bookDel(row.addressBookId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.title = "新增外部通讯录";
      this.resetForm("form");
      this.disabled = false;
      this.open = true;
    },
    cancel() {
      this.open = false;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      bookList(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style lang="scss">
::v-deep.tox-tinymce-aux {
  z-index: 9999 !important;
}
</style>
